/*
 * @Author: Elvis shuaiqy@hotmail.com
 * @Date: 2022-07-24 10:48:44
 * @LastEditors: Elvis shuaiqy@hotmail.com
 * @LastEditTime: 2022-07-24 11:43:15
 * @FilePath: /pro-lowcode-runtime-front/src/utils/core/refreshFromOpener.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
type CrossDomainMessageType = {
	status: string
	action: string
	system: string
	options: any
}

export function tryRefreshOpener(message: CrossDomainMessageType) {
	if (window.opener !== undefined && window.opener !== null) {
		window.opener.postMessage(message, '*')
	} else if (window.parent !== undefined && window.parent !== null) {
		window.parent.postMessage(message, '*')
	}
}

export function corssDomainMessageProcessor() {
	window.addEventListener(
		'message',
		function ({ data }: { data: CrossDomainMessageType }) {
			if (data.system === 'baiteda-runtime') {
				switch (data.action) {
					case 'processcenter-list-update':
						window.updatePortalList()
						break
				}
			}
		}
	)
}
